package InputStream;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class Test_IO {
    public static void main(String[] args) throws Exception {
//            File file = new File("E:\\360downloads\\a.txt");
//        System.out.println(file.getName());
//        System.out.println(file.getAbsolutePath());
//        System.out.println(file.getParent());
//        System.out.println(file.length());
//        System.out.println(file.isFile());
//        System.out.println(file.exists());
//        System.out.println(file.isDirectory());
//        int read;
//        FileInputStream fileInputStream=null;
//        try {
//            fileInputStream = new FileInputStream("E:\\a.txt");
//            while (( read=fileInputStream.read()) != -1){
//                System.out.print((char)read);
//            }
//        }
//        catch(IOException e){
//            e.printStackTrace();
//        }finally {
//            try {
//                fileInputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }

//        FileOutputStream fileOutputStream=null;
//        try {
//            fileOutputStream=new FileOutputStream("E:\\a.txt");
//            String s="21黄慧鹏";
//            fileOutputStream.write(s.getBytes());//String--->byte[]
////            fileOutputStream.write(s.getBytes(StandardCharsets.UTF_8),0,10);//byte[]的10字节，汉子一个字节
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                fileOutputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    FileOutputStream fileOutputStream=null;
//    FileInputStream fileInputStream=null;
//    int realLen=0;
//    byte[] data = new byte[1024];
//    try {
//            fileInputStream=new FileInputStream("C:\\Users\\Administrator\\Pictures\\hhp.png");
//            fileOutputStream=new FileOutputStream("E:\\b.png");
//        while ((realLen = fileInputStream.read(data))!=-1){
//            System.out.println("每次真正读取的字节数："+realLen);
//            fileOutputStream.write(data,0,realLen);
//        }
//    } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                fileInputStream.close();
//                fileOutputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        FileReader fileReader = null;
//        char [] data = new char[1024];
//        int realLen=0;
//        try {
//            fileReader = new FileReader("E:\\a.txt");
//            //一个字符一个字符读取
////             while ((data = fileReader.read())!=-1){
////                System.out.print((char)data);
////            }
//            //读到字符数组里
//            while ((realLen = fileReader.read(data))!=-1){ //在data【】里读了多少各字符
//                System.out.print(new String(data,0,realLen));
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                fileReader.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        FileWriter fileWriter = null;
//        char[] data = {'a','b'};
//        try {
//            fileWriter = new FileWriter("E:\\b.txt",true);
//            fileWriter.write('H');
//            fileWriter.write("asdasdas");
//            fileWriter.write(65);
//            fileWriter.write(data);
//            fileWriter.write("12345",2,3);
//
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                //关闭流，自动把数据从内存写入磁盘，不然写不到磁盘里
//                fileWriter.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        BufferedReader bufferedReader = null;
//        String line;
//        bufferedReader=new BufferedReader(new FileReader("E:\\a.txt"));
//        while ((line = bufferedReader.readLine() )!= null) {//每次读一行，没有数据返回null
//            System.out.println(line);
//        }
//        bufferedReader.close();
//        BufferedWriter bufferedWriter = null;
//        bufferedWriter = new BufferedWriter(new FileWriter("E:\\b.txt",true));
//        bufferedWriter.write("黄慧鹏adakjfkashfjk");
//        bufferedWriter.newLine();//会根据系统自动换行
//        bufferedWriter.write("黄慧鹏adakjfkashfjk");
//        bufferedWriter.close();
//        BufferedWriter bufferedWriter;
//        BufferedReader bufferedReader;
//        bufferedReader=new BufferedReader(new FileReader("E:\\a.txt"));
//        bufferedWriter = new BufferedWriter(new FileWriter("E:\\b.txt"));
//        String line;
//        while ((line = bufferedReader.readLine())!=null){
//            bufferedWriter.write(line);
//            bufferedWriter.newLine();
//        }
//        bufferedReader.close();
//        bufferedWriter.close();
//        BufferedInputStream bufferedInputStream;
//        BufferedOutputStream bufferedOutputStream;
//        bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Pictures\\hhp.png"));
//        bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\b.png"));
//        byte[] data =new byte[512];
//        int len=0;
//        while ((len = bufferedInputStream.read(data))!=-1){
//            bufferedOutputStream.write(data,0,len);
//        }
//        bufferedInputStream.close();;
//        bufferedOutputStream.close();
//        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("E:\\b.txt"));
//        objectOutputStream.writeInt(100);
//        objectOutputStream.writeBoolean(true);
//        objectOutputStream.writeDouble(1.1);
//        objectOutputStream.writeChar('a');
//        objectOutputStream.writeObject(new Dog("小黑"));
//        objectOutputStream.close();
//        ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("E:\\b.txt"));
//        System.out.println(objectInputStream.readInt());
//        System.out.println(objectInputStream.readBoolean());
//        System.out.println(objectInputStream.readDouble());
//        System.out.println(objectInputStream.readChar());
//        System.out.println(objectInputStream.readObject().toString());
//        objectInputStream.close();

        //输入字节流---->Reader
//        InputStreamReader inputStreamReader =
//                new InputStreamReader(new FileInputStream("E:\\b.txt"),"utf-8");
//        //Reader赋值给BufferedReader
//        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//                BufferedReader bufferedReader =
//                        new BufferedReader(
//                                new InputStreamReader(
//                                        new FileInputStream("E:\\b.txt")));
//        String line;
//        while ((line = bufferedReader.readLine())!=null){
//            System.out.println(line);
//        }
//            OutputStreamWriter outputStreamWriter =
//                    new OutputStreamWriter(
//                            new FileOutputStream("E:\\b.txt",true),"gbk");
//            outputStreamWriter.write("黄慧鹏hhp最帅！");
//        outputStreamWriter.close();
//        PrintStream printStream = new PrintStream(System.out);
//        printStream. print("xxxx");
//        printStream. write("sdadas".getBytes(StandardCharsets.UTF_8));

//        PrintWriter printWriter = new PrintWriter(System.out);
//        printWriter.print("aa");
//        printWriter.close();
//        BufferedReader bufferedReader = new BufferedReader(
//                new FileReader("src\\InputStream\\db.properties"));
//        String line;
//        while((line = bufferedReader.readLine())!=null){
//            String[] split = line.split("=");
//            if ("ip".equals(split[0]))  //自己的笨重办法
//            System.out.println(split[0] + "--->" + split[1]);
//        }
//        Properties properties = new Properties();
//        properties.load(new FileReader("src\\InputStream\\db.properties"));
//        //k-v显示在控制台
//        properties.list(System.out);
//
//        System.out.println(properties.getProperty("ip"));
//        System.out.println(properties.getProperty("user"));

//        Properties properties = new Properties();
//        properties.setProperty("user","黄慧鹏");
//        properties.setProperty("ip","122");
//        properties.setProperty("a","122");
//        properties.store(
//                new FileWriter("src\\InputStream\\db1.properties"),null);
        //创建目录
//        File file = new File("e:\\aaaa");
//        if(!file.exists()){
//            if(file.mkdirs()){
//                System.out.println("ok");
//            }else System.out.println("no");
//        }
//        //创建文件
//        file = new File("e:\\aaaa\\a.txt");
//        if(!file.exists()){
//            if(file.createNewFile()){
//                System.out.println("okk");
//                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("e:\\aaaa\\a.txt"));
//                bufferedWriter.write("黄慧鹏真帅，个子还高");
//                bufferedWriter.close();
//            }else System.out.println("noo");
//        }
//        BufferedReader bufferedReader = new BufferedReader(new FileReader("e:\\b.txt"));
//        String s;
//        int count = 0;
//        while ((s = bufferedReader.readLine())!=null){
//            System.out.println(++count + s);
//        }
//        bufferedReader.close();
        Properties properties = new Properties();
        properties.load(new FileReader("e:\\b.txt"));
        String name = properties.getProperty("name");
//        System.out.println(name);//null ??
        String age = properties.getProperty("age");
        String height = properties.getProperty("身高");
        shuaiGe shuaiGe = new shuaiGe(name, Integer.parseInt(age), Float.parseFloat(height));
        System.out.println(shuaiGe);

        ObjectOutputStream objectOutputStream =
                new ObjectOutputStream(
                        new FileOutputStream("e:\\c.txt"));
        objectOutputStream.writeObject(shuaiGe);
        objectOutputStream.close();

        ObjectInputStream objectInputStream =
                new ObjectInputStream(
                        new FileInputStream("e:\\c.txt"));
        shuaiGe =(shuaiGe) objectInputStream.readObject();
        System.out.println(shuaiGe);


    }//main
        }
        class shuaiGe implements Serializable{
            String name;
            int age;
            float height;

            public shuaiGe(String name, int age, float height) {
                this.name = name;
                this.age = age;
                this.height = height;
            }

            @Override
            public String toString() {
                return "shuaiGe{" +
                        "name='" + name + '\'' +
                        ", age=" + age +
                        ", height=" + height +
                        '}';
            }
        }

class Dog implements Serializable{
    String name;
    public Dog(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                '}';
    }
}